package xiaolong.lian.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import xiaolong.lian.entity.Article;
import xiaolong.lian.service.ArticleService;

import java.util.List;

@Controller
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    //http://localhost:8080/article/search/%E9%93%81
    @RequestMapping(value = "/search/{keyword}", method = RequestMethod.GET)
    public String searchByTitle(@PathVariable("keyword") String keyword, Model model){
        List<Article> articles = articleService.getByTitleKeyword(keyword);
        model.addAttribute("articles", articles);
        return "search"; // WEB-INF/jsp/search.jsp
    }

    //http://localhost:8080/article/search.json?keyword=%E9%93%81
    @RequestMapping(value = "/search.json",
            method = RequestMethod.GET,
            produces = "application/json; charset=utf-8"
    )
    @ResponseBody
    public String searchByTitleToJson(@RequestParam("keyword") String keyword){
        return articleService.getByTitleKeyword(keyword).toString();
    }
}
